package com.iotechn.unimall.admin.api.server;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.biz.service.service.ServiceBizService;
import com.iotechn.unimall.data.domain.ServerDO;
import com.iotechn.unimall.data.dto.product.SpuDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ServerMapper;
import com.iotechn.unimall.data.vo.SelectVO;
import com.iotechn.unimall.data.vo.ServerVO;
import com.iotechn.unimall.data.vo.TreeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, ServerDO> implements ServerService {

    @Resource
    private ServiceBizService serviceBizService;

    @Override
    public Page<ServerVO> page(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        QueryWrapper<ServerDO> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(name)) {
            wrapper.lambda().like(ServerDO::getName, name);
        }
        wrapper.lambda().orderByDesc(ServerDO::getGmtCreate);
        List<ServerDO> serverDOS = baseMapper.selectList(wrapper);
        Integer count = baseMapper.selectCount(wrapper);
        List<ServerVO> serverVOS = selectRegion(serverDOS, 0L);
        return new Page<>(serverVOS,page,limit,count);
    }

    private List<ServerVO> selectRegion(List<ServerDO> treeServerDOS, Long pid) {
        List<ServerVO> result = new ArrayList<>();
        for (ServerDO item: treeServerDOS) {
            if (item.getPid().equals(pid)) {
                ServerVO vo = new ServerVO();
                BeanUtil.copyProperties(item,vo);
                vo.setChildren(selectRegion(treeServerDOS, item.getId()));
                result.add(vo);
            }
        }
        return result;
    }

    @Override
    public String create(ServerDO serverDO, Long adminId) throws ServiceException {
        serverDO.setGmtCreate(new Date());
        if (baseMapper.insert(serverDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String update(ServerDO serverDO, Long adminId) throws ServiceException {
        serverDO.setGmtUpdate(new Date());
        if (baseMapper.updateById(serverDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String delete(Long id, Long adminId) throws ServiceException {
        // 判断是否有子集
        QueryWrapper<ServerDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ServerDO::getPid, id);
        List<ServerDO> serverDOS = baseMapper.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(serverDOS)) {
            throw new AdminServiceException(ExceptionDefinition.SERVER_EXIST_SUB);
        }
        if (baseMapper.deleteById(id) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public List<ServerDO> query(Long id, Long adminId) throws ServiceException {
        QueryWrapper<ServerDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ServerDO::getPid, id);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<TreeVO> tree(Long adminId) throws ServiceException {
        List<ServerDO> serverDOS = baseMapper.selectList(null);
        return serviceBizService.selectRegion(serverDOS,0L);
    }
}
